# -*- coding: utf-8 -*-

from gtk import *
from includes import *
from urllib import unquote, urlretrieve
from urlparse import urlparse
from re import *
from time import *
from threading import *
import pango
from actions import *

class PhotoView(ScrolledWindow):
	def __init__(self, ppm):
		ScrolledWindow.__init__(self)
		self.ppm = ppm
		self.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC)
		
		self.iv = IconView(ppm.list)
		#self.iv.set_item_width(THUMB_SIZE)
		self.iv.set_selection_mode(SELECTION_MULTIPLE)
		#self.iv.set_reorderable(True)
		
		cell_thumb = CellRendererPixbuf()
		cell_thumb.set_property("xalign", 0.5)
		cell_thumb.set_property("yalign", 1)
		cell_thumb.set_property("width", THUMB_SIZE)
		cell_thumb.set_property("follow-state", True)
		self.iv.pack_end(cell_thumb)
		self.iv.add_attribute(cell_thumb, "pixbuf", COLUMN_THUMB)

		cell_name = CellRendererText()
		cell_name.set_property("wrap-mode", pango.WRAP_WORD_CHAR)
		cell_name.set_property("wrap-width", THUMB_SIZE)
		cell_name.set_property("width", THUMB_SIZE)
		cell_name.set_property("yalign", 0.5)
		cell_name.set_property("alignment", pango.ALIGN_CENTER)
		self.iv.pack_end(cell_name)
		self.iv.add_attribute(cell_name, "text", COLUMN_NAME)
		
		
		cell_nb_copy = CellRendererSpin()
		cell_nb_copy.set_property("wrap-mode", pango.WRAP_WORD_CHAR)
		cell_nb_copy.set_property("wrap-width", THUMB_SIZE)
		cell_nb_copy.set_property("width", THUMB_SIZE)
		cell_nb_copy.set_property("yalign", 0)
		cell_nb_copy.set_property("adjustment", Adjustment(lower=0, upper=100, step_incr=1, page_incr=10))
		cell_nb_copy.set_property("alignment", pango.ALIGN_CENTER)
		cell_nb_copy.set_property("editable", True)
		def on_editing_started(cellrenderer, editable, path):
			editable.set_numeric(True)
			#editable.set_update_policy(UPDATE_IF_VALID)
			#editable.set_snap_to_ticks(True)
			#editable.set_wrap(True)
		cell_nb_copy.connect("editing-started", on_editing_started)
		
		def on_edited(cellrenderertext, path, new_text, model):
			model.get(int(path)).nb_copy = clamp(int(new_text), 0, 100)
			model.row_changed(path, model.get_iter(path))
		
		
		cell_nb_copy.connect('edited', on_edited, self.ppm.list)
		
		self.iv.pack_end(cell_nb_copy)
		self.iv.add_attribute(cell_nb_copy, "markup", COLUMN_NB_COPY_TEXT)
		
		
		self.iv.set_property("has-tooltip", True)
		self.iv.connect("query-tooltip", self.on_query_tooltip)
		
		self.add(self.iv)
		
		self.drag_dest_set(DEST_DEFAULT_ALL, (("text/uri-list", 0, 0) , ("text/plain", 0, 0)), gdk.ACTION_COPY | gdk.ACTION_MOVE)
		self.connect("drag-data-received", self.on_drag_data_received)
		
	def on_query_tooltip(self, widget, x, y, keyboard_mode, tooltip):
		path = widget.get_path_at_pos(int(x + self.get_hadjustment().get_value()), int(y + self.get_vadjustment().get_value()))
		if path:
			photo = widget.get_model()[path]
			hbox = HBox()
			hbox.pack_start(image_new_from_pixbuf(photo[COLUMN_THUMB]))
			lab = Label("<b><big>" + photo[COLUMN_NAME] + "</big></b>\n<i>Format : </i>" + photo[COLUMN_TYPE].upper() + "\n<i>Résolution : </i>" + str(photo[COLUMN_WIDTH]) + " x " + str(photo[COLUMN_HEIGHT]) + " pixels")
			lab.set_use_markup(True)
			hbox.pack_start(lab, padding=5)
			hbox.show_all()
			tooltip.set_custom(hbox)
			return True
		else:
			return False
			
	
	def on_drag_data_received(self, widget, drag_context, x, y, selection_data, info, timestamp):
		photos = []
		for f in selection_data.data.splitlines():
			if f == "\0" :
				continue
			try :
				photos.append(unquote(urlretrieve(f)[0]))
			except IOError:
				photos.append(sub("^/([A-Z]:/.*)", "\\1", unquote(urlparse(f).path)))
		self.ppm.create_handler()
		self.ppm.handler.add_photos(photos)
		self.ppm.start_handler()
		drag_context.finish(True, False, timestamp)

